package zozopo.warehouse.operations;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import zozopo.database.BusiDatabaseManager;
import zozopo.database.SqlMachine;
import zozopo.operations.comm.OperatingService;

public class GoodsclasslistOS extends OperatingService{
	private static final Log logger = LogFactory.getLog(GoodsclasslistOS.class);
	
	public GoodsclasslistOS(Connection conn){
		super(conn);
	}
	
	public GoodsclasslistOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean insert(GoodsclasslistTB goodsclasslist){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> insertMap = new HashMap<String, Object>();

		insertMap.put(GoodsclasslistTB.GOODSCLASSID_GOODSCLASSLIST, goodsclasslist.getGoodsclassid_goodsclasslist());
		insertMap.put(GoodsclasslistTB.GOODSCLASSNAME_GOODSCLASSLIST, goodsclasslist.getGoodsclassname_goodsclass());
		insertMap.put(GoodsclasslistTB.GOODSID_GOODSCLASSLIST, goodsclasslist.getGoodsid_goodsclasslist());

		return sqlMachine.insertRow(GoodsclasslistTB.TABLE_NAME, insertMap);
	}
	
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<GoodsclasslistTB> selectListBySql(String sql){
		List<GoodsclasslistTB> list = null;
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		
		if(resultSet != null){
				try {
					list = new ArrayList<GoodsclasslistTB>();
					while(resultSet.next()){
						GoodsclasslistTB goodsclasslist = new GoodsclasslistTB();
						goodsclasslist.setGoodsclassid_goodsclasslist(resultSet.getInt(GoodsclasslistTB.GOODSCLASSID_GOODSCLASSLIST));
						goodsclasslist.setGoodsid_goodsclasslist(resultSet.getInt(GoodsclasslistTB.GOODSID_GOODSCLASSLIST));
						goodsclasslist.setId_goodsclasslist(resultSet.getInt(GoodsclasslistTB.ID_GOODSCLASSLIST));
						
						goodsclasslist.setGoodsclassname_goodsclass(resultSet.getString(GoodsclasslistTB.GOODSCLASSNAME_GOODSCLASSLIST));

						list.add(goodsclasslist);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		return list;
	}
	
	public List<GoodsclasslistTB> select(){
		
		List<GoodsclasslistTB> list = null;
		String sql = "SELECT * FROM " + GoodsclasslistTB.TABLE_NAME; 
		list = selectListBySql(sql);
		
		return list;
	}
	
	public List<GoodsclasslistTB> selectByGoodsclassidStr(String goodsclassidstr){
		
		List<GoodsclasslistTB> list = null;
		String sql = "SELECT * FROM " + GoodsclasslistTB.TABLE_NAME
		+ " where " + GoodsclasslistTB.GOODSCLASSID_GOODSCLASSLIST + " IN " + goodsclassidstr; 
		list = selectListBySql(sql);
		
		return list;
	}
	
	
	
	public GoodsclasslistTB getById(int id){
		
		String sql = "SELECT * FROM " + GoodsclasslistTB.TABLE_NAME + " where " + GoodsclasslistTB.ID_GOODSCLASSLIST + "=" + id;
		GoodsclasslistTB goodsclasslist = new GoodsclasslistTB();
		List<GoodsclasslistTB> goodsclassList = selectListBySql(sql);
		
		goodsclasslist = goodsclassList.get(0);
		
		return goodsclasslist;
	}
	
	/**
	 * @version 1.0
	 * @author gee
	 */
	public boolean deleteAll(int goodsid){
		String sql = "delete from "+GoodsclasslistTB.TABLE_NAME+" where "+GoodsclasslistTB.GOODSID_GOODSCLASSLIST+"="+goodsid+"";
		return this.databaseConnection.executeUpdate(conn,sql);
	}
	
	public LinkedHashMap<Integer,Integer>  getClassNGoodsidMap(int goodsid){
		
		String sql = "SELECT * FROM " + GoodsclasslistTB.TABLE_NAME + " where " + GoodsclasslistTB.GOODSID_GOODSCLASSLIST + "=" + goodsid;
		List<GoodsclasslistTB> goodsclasslistList = selectListBySql(sql);
		LinkedHashMap<Integer,Integer> goodsclasslistMap = new LinkedHashMap<Integer,Integer>();
		
		for(GoodsclasslistTB goodsclasslist:goodsclasslistList){
			goodsclasslistMap.put(goodsclasslist.getGoodsclassid_goodsclasslist(), goodsclasslist.getGoodsid_goodsclasslist());
		}
		
		return goodsclasslistMap;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public Map<Integer,List<GoodsclasslistTB>> getGoodsclassInfoMapList(List<GoodsTB> goodsList){
		
		
		if(goodsList.size() <=0){
			return null;
		}
		
		StringBuffer inStr = new StringBuffer();
		inStr.append("(");
		
		boolean isFirst = true;
		for(GoodsTB goods:goodsList){
			if(isFirst){
				isFirst = false;
				inStr.append("" + goods.getId_goods());
			}else{
				inStr.append("," + goods.getId_goods());
			}
			
		}
		inStr.append(")");
		
		String sql = "SELECT * FROM goodsclasslist_warehouse "
			+ "LEFT JOIN goodsclass_warehouse ON goodsclassid_goodsclasslist=id_goodsclass " 
			+ "WHERE goodsid_goodsclasslist IN " + inStr;
		
		List<GoodsclasslistTB> list = selectListBySql(sql);
		
		
		Map<Integer,List<GoodsclasslistTB>> listMap = new  HashMap<Integer,List<GoodsclasslistTB>>();
		
		List<GoodsclasslistTB> list2 = new ArrayList<GoodsclasslistTB>();
		int lastId = -1;
		
		for(GoodsclasslistTB goodsclasslist:list){
			
			
			
			if(lastId != -1 && lastId != goodsclasslist.getGoodsid_goodsclasslist()){
				listMap.put(lastId,list2);	
				
				list2 = new ArrayList<GoodsclasslistTB>();
				list2.add(goodsclasslist);
				lastId = goodsclasslist.getGoodsid_goodsclasslist();
				
			}else if(lastId != goodsclasslist.getGoodsid_goodsclasslist()){
				
				list2 = new ArrayList<GoodsclasslistTB>();
				list2.add(goodsclasslist);
				lastId = goodsclasslist.getGoodsid_goodsclasslist();
				
			}else if(lastId == goodsclasslist.getGoodsid_goodsclasslist()){
				
				list2.add(goodsclasslist);
				lastId = goodsclasslist.getGoodsid_goodsclasslist();
			}
		}
		listMap.put(lastId,list2);	
		
		
		return listMap;
	}

}
